<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"  %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form role="form" data-toggle="validator" id="form" name="form" >

<c:set var="docu_type" value="W" />
<c:if test="${not empty orderMap}">
	<c:set var="docu_type" value="M" />
	<div id="modal-title" style="display:none;">${orderMap.PRD_NAME} (${orderMap.PRD_ID})</div>
</c:if>
<input type="hidden" id="docu_type" name="docu_type" value="${docu_type}" />

<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-danger">
			<div class="panel-heading">
            	<i class="glyphicon glyphicon-alert"></i>  <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">안내 (클릭하면 닫힙니다.)</a>
			</div>
			<div id="collapseOne" class="panel-collapse collapse in">
				<div class="panel-body">
					<div class="row">
						<div class="col-lg-12">
							<i class="fa fa-question-circle fa"></i> <strong>입고 확정일이 입력이 되면?</strong>
							 <br/>&nbsp;&nbsp;- 발주 상태에서 입고(재고) 상태로 자동 변경됩니다.
							 <br/>&nbsp;&nbsp;- 지불방법,지불일자 이외에 더이상 해당 발주건에 대한 수정이 불가능합니다.
						</div>
					</div>
					<div class="row">
						<div class="col-lg-12">
							&nbsp;
						</div>
					</div>
					<div class="row">
						<div class="col-lg-12">
							<i class="fa fa-question-circle fa"></i> <strong>최종 완료 버튼을 누르게 되면?</strong>
							 <br/>&nbsp;&nbsp;- 입고 확정일, 지불 일자까지 모두 입력된 상태에서 활성화됩니다.
							 <br/>&nbsp;&nbsp;- 해당 버튼을 눌러 확정을 해야만 발주에 대한 프로세스가 완료됩니다.
							 <br/>&nbsp;&nbsp;- 버튼을 누르지 않는 경우에 미확정 발주로 남아있게 되므로 반드시 완료해주시기 바랍니다.
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
	
<div class="row">
	<div class="col-lg-12">
		
		<div class="panel panel-info">
			<div class="panel-heading">
            	발주 정보
			</div>
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-4">
					
						<div class="form-group">
						    <label class="control-label" id="prd_id_desc">* 상품 <c:if test="${not empty orderMap.PRD_ID}">: ${orderMap.PRD_ID}</c:if></label>
						    <div class="input-group">
						    	<span class="input-group-btn" >
						        	<a id="searchBtn" class="btn btn-warning" type="button"><i class="fa fa-search fa"></i></a>
						      	</span>
						      	<input type="hidden" id="prd_id" name="prd_id" value="${orderMap.PRD_ID}" />
						      	<input type="hidden" id="cus_id" name="cus_id" value="${orderMap.CUS_ID}" />
							    <input type="text" class="form-control" id="prd_name" name="prd_name" value="${orderMap.PRD_NAME}"
							    										data-error="상품을 선택해주세요."
							    										required="required">
							</div>
						    <div class="help-block with-errors">발주 상품을 검색 후 선택</div>
						</div>
						
						
						<div class="form-group">
						    <label class="control-label">* 수량</label>
						    <input type="text" class="form-control" id="ord_quantity" name="ord_quantity" value="<fmt:formatNumber value="${orderMap.ORD_QUANTITY}" />"
						    											maxlength=12
		 					    										onkeyup="onlyNumber(this);"
		 					    										onblur="minNumber(this, '1'); onlyNumber(this); comma(this);"
		 					    										required="required">
						    <div class="help-block with-errors">최소 1이상을 입력</div>
						</div>
						
						<div class="form-group">
						    <label class="control-label">* 단가</label>
						    <div class="input-group">
						    	<span class="input-group-addon">￦</span>
							    <input type="text" class="form-control" id="ord_unit_price" name="ord_unit_price" value='<fmt:formatNumber value="${orderMap.ORD_UNIT_PRICE}" />'
		 					    										maxlength=12
		 					    										onkeyup="onlyNumber(this);"
		 					    										onblur="onlyNumber(this); comma(this);"
		 					    										required="required">
						    </div>
						    <div class="help-block with-errors">상품 먼저 선택 후 입력</div>
						</div>
						<div class="form-group">
						    <label class="control-label">* 운송비</label>
						    <div class="input-group">
						    	<span class="input-group-addon">￦</span>
							    <input type="text" class="form-control" id="ord_delivery_price" name="ord_delivery_price" value='<fmt:formatNumber value="${orderMap.ORD_DELIVERY_PRICE}" />'
		 					    										maxlength=12
		 					    										onkeyup="onlyNumber(this);"
		 					    										onblur="onlyNumber(this); comma(this);"
		 					    										required="required">
						    </div>
						    <div class="help-block with-errors">반드시 숫자만 입력</div>
						</div>
					</div>
					<div class="col-lg-4">
						<div class="form-group">
						    <label class="control-label">* 기타비용</label>
						    <div class="input-group">
						    	<span class="input-group-addon">￦</span>
							    <input type="text" class="form-control" id="ord_etc_price" name="ord_etc_price" value='<fmt:formatNumber value="${orderMap.ORD_ETC_PRICE}" />'
		 					    										maxlength=12
		 					    										onkeyup="onlyNumber(this);"
		 					    										onblur="onlyNumber(this); comma(this);"
		 					    										required="required">
						    </div>
						    <div class="help-block with-errors">반드시 숫자만 입력</div>
						</div>
						<div class="form-group">
							<label class="control-label">* 부가세 포함 여부</label>
							(
							<label class="radio-inline">
								<input type="radio" name="ord_tax_flag" id="ord_tax_flag" value="Y" <c:if test="${empty orderMap || orderMap.ord_tax_flag eq 'Y'}">checked</c:if>>적용
					   		</label>
					   		<label class="radio-inline">
                                <input type="radio" name="ord_tax_flag" id="ord_tax_flag" value="N" <c:if test="${orderMap.ord_tax_flag eq 'N'}">checked</c:if>>미적용
                            </label>
                            )
                            <div class="input-group">
                            	<c:choose>
                            		<c:when test="${empty orderMap}">
                            			<c:set var="ord_tex_per" value="10" />
                            		</c:when>
                            		<c:otherwise>
                            			<c:set var="ord_tex_per" value="${orderMap.ORD_TAX_PER}" />
                            		</c:otherwise>
                            	</c:choose>
							    <input type="text" class="form-control" id="ord_tax_per" name="ord_tax_per" value="${ord_tex_per}"
							    										maxlength=2
		 					    										onkeyup="onlyNumber(this);"
		 					    										onblur="onlyNumber(this);"
		 					    										data-error="부가세율을 입력해주세요."
		 					    										required="required">
		 					   	<span class="input-group-addon">%</span>
						    </div>
						    <div class="help-block with-errors">반드시 숫자만 입력</div>
						</div>
                        <div class="form-group">
						    <label class="control-label">* 입고 예정일</label>
						   	<div class='input-group date' id='div_ord_plan_date'>
						   		<input type='text' class="form-control readonly" id="ord_plan_date" name="ord_plan_date" value="${orderMap.ORD_PLAN_DATE}"
						   												maxlength=10
						   												data-error="입고 예정일을 선택 해주세요."  
						   												required="required">
						   		<span class="input-group-addon">
						   			<i class="glyphicon glyphicon-calendar"></i>
								</span>
						   	</div>
						   	<div class="help-block with-errors">입고 예정 일자</div>
						</div>
						 <div class="form-group">
						    <label class="control-label">입고 확정일</label>
						   	<div class='input-group date' id='div_ord_real_date'>
						   		<input type='text' class="form-control readonly" id="ord_real_date" name="ord_real_date" value="${orderMap.ORD_PLAN_DATE}"
						   												maxlength=10
						   												<c:if test="${empty orderMap}">disabled</c:if>>
						   												
						   		<span class="input-group-addon">
						   			<i class="glyphicon glyphicon-calendar"></i>
								</span>
						   	</div>
						   	<div class="help-block with-errors">실제 입고 일자 (입고 후 입력)</div>
						</div>
					</div>
					<div class="col-lg-4">
						<div class="form-group">
						   <label class="control-label">지불 방법</label>
						   <select class="form-control" id="ord_payment_type" name="ord_payment_type">
						   		<option value="">선택</option>
	                              <c:forEach items="${paymentTypeList}" var="list">
	                              	<option <c:if test="${orderMap.ORD_PAYMENT_TYPE eq list.CODE}">selected="selected"</c:if> value="${list.CODE}">${list.NAME}</option>
	                              </c:forEach>
	                          </select>
	                          <div class="help-block with-errors">대금 지불 방법 선택</div>
						</div>
						<div class="form-group">
						    <label class="control-label">지불 일자</label>
						   	<div class='input-group date' id='div_ord_payment_date'>
						   		<input type='text' class="form-control readonly" id="ord_payment_date" name="ord_payment_date" value="${orderMap.ORD_PAYMENT_DATE}"
						   												maxlength=10>
						   		<span class="input-group-addon">
						   			<i class="glyphicon glyphicon-calendar"></i>
								</span>
						   	</div>
						   	<div class="help-block with-errors">&nbsp;</div>
						</div>
						<div class="form-group">
						    <label class="control-label">* 발주 ID</label>
						    <input type="text" class="form-control" id="prd_id" name="prd_id" placeholder="자동 생성" value="${orderMap.PRD_ID}"
						    										readonly="readonly">
						    <div class="help-block with-errors">등록시 자동 생성</div>
						</div>
						<div class="form-group">
						    <label class="control-label">발주 확정</label>
						    <div class="input-group">
						    	<span class="input-group-btn" >
						        	<button class="btn btn-danger" type="button" style="width:100%;"
						        			<c:if test="${empty orderMap || empty orderMap.ORD_REAL_DATE || empty orderMap.ORD_PAYMENT_DATE}">disabled</c:if>
						        			>최종 완료 (확정)</button>
						      	</span>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-lg-12">
						<div class="form-group">
							<label>메모</label>
							<textarea class="form-control" rows="3" id="memo" name="memo">${customerMap.MEMO}</textarea>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="row">
	<div class="col-lg-12" style="text-align:center;">
		<c:choose>
			<c:when test="${empty orderMap}">
				<button type="submit" class="btn btn-success" 
									  data-toggle="confirmation" 
									  title="<spring:message code='comm.write.confirm' />" 
									  data-btn-ok-label="<spring:message code='comm.btn.continue' />"
									  data-btn-cancel-label="<spring:message code='comm.btn.cancle' />">등록</button>
			</c:when>
			<c:otherwise>
				<button type="submit" class="btn btn-primary"
									  data-toggle="confirmation" 
									  title="<spring:message code='comm.modify.confirm' />" 
									  data-btn-ok-label="<spring:message code='comm.btn.continue' />"
									  data-btn-cancel-label="<spring:message code='comm.btn.cancle' />">수정</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">닫기</button>
			</c:otherwise>
		</c:choose>
	</div>
</div>
		
</form>

<style>
	#layerpop .modal-dialog {
		width : 60%;
	}
</style>
<div class="modal fade" id="layerpop">
	<div class="modal-dialog">
		<div class="modal-content">
		</div>
	</div>
</div>


<script type="text/javascript">
	$(function() {
		
		$('#div_ord_plan_date').datepicker({
			autoclose: true,
		    format: "yyyy-mm-dd",
		    language: "kr",
		    todayHighlight: true,
		    toggleActive: true
		});
		<c:if test="${not empty orderMap}">
		$('#div_ord_real_date').datepicker({
			autoclose: true,
		    format: "yyyy-mm-dd",
		    language: "kr",
		    todayHighlight: true,
		    toggleActive: true
		});
		</c:if>
		$('#div_ord_payment_date').datepicker({
			autoclose: true,
		    format: "yyyy-mm-dd",
		    language: "kr",
		    todayHighlight: true,
		    toggleActive: true
		});
		
		
		$('input:radio[name="ord_tax_flag"]').click(function() {
			if ($(this).val() == 'N') {
				$('#ord_tax_per').val("0");
				$('#ord_tax_per').attr("readonly", true);
			} else {
				<c:if test="${orderMap.ORD_TAX_PER ne '0'}">
					$('#ord_tax_per').val("${orderMap.ORD_TAX_PER}");
				</c:if>
				<c:if test="${empty orderMap}">
				$('#ord_tax_per').val("10");
				</c:if>
				$('#ord_tax_per').attr("readonly", false);
			}
		});
			
	});
	
	
	$(document).on("focusin", "#prd_name", function(event) {
		$('#form').find("[name=prd_name]").prop('readonly', true);
	});

	$(document).on("focusout", "#prd_name", function(event) {
		$('#form').find("[name=prd_name]").prop('readonly', false);
	});
	
	$(document).on("focusin", "#ord_unit_price", function(event) {
		if ($('#prd_id').val() == '') {
			$('#form').find("[name=ord_unit_price]").prop('readonly', true);
		} else {
			$('#form').find("[name=ord_unit_price]").prop('readonly', false);
		}
	});


	$('[data-toggle=confirmation]').confirmation();

	$('#searchBtn').on("click", function() {

		// SESSION 여부 확인
		if (!isSession()) {
			bs_alert.modal("<spring:message code='comm.session.expire' />", "info","LOGOUT");
		} else {
			$('#layerpop').modal({
				remote: "/product/searchPopup.ur?layout=N"
			})
		}
		
	});
	
	$(document).ready(function() {
		
		$('#form').validator().on('submit', function(e) {	
			
			if (e.isDefaultPrevented()) {
				bs_alert.modal("<spring:message code='comm.field.empty.error' />", "info");
			} else {
				
				e.preventDefault();
				
				var url = "/order/submitRegister.ur";
				var action_flag = "RELOAD";
				<c:if test="${not empty orderMap}">
					url = "/order/submitModify.ur";
					action_flag = "LIST";
				</c:if>
					
				$.ajax({
			        url: url,
			        type: "POST",
			        data: $(this).serializeArray(),
			        success: function(data, status, xhr) {
						switch(data) {
							case 0 : 
								bs_alert.modal("<spring:message code='comm.modify.success' />", "success", action_flag);
								break;
							case 9 : 
								bs_alert.modal("<spring:message code='comm.request.data.error' />", "warning", action_flag);
								break;
							case 1 :
								bs_alert.modal("<spring:message code='comm.db.modify.error' />", 'warning', action_flag);
								break;
							case 8 :
								bs_alert.modal("<spring:message code='comm.unknown.error' />", 'warning', action_flag);
								break;
						}
			        },
			        error: function(xhr, status, err) {
			            // do the error stuff
			        }
			    });
				
			}
		});
	});
</script>